from django.db import models
import json
from datetime import date
from datetime import datetime
# Create your models here.


# 网站会员模型
class User(models.Model):
    # 用户名
    username = models.CharField(max_length=30, null=False, verbose_name='用户名')
    # 登录密码
    password = models.CharField(max_length=80, null=False, verbose_name='登录密码')
    # 真实姓名
    truename = models.CharField(max_length=20, default='', verbose_name='真实姓名')
    # 性别
    sex = models.CharField(max_length=1, default='男', verbose_name='性别')
    # 年龄
    age = models.IntegerField(default=18, verbose_name='年龄')
    # 地址
    u_address = models.CharField(max_length=100, default='', verbose_name='地址')
    # 手机号
    u_phone = models.CharField(max_length=11, default='', verbose_name='手机号')
    # 邮箱
    u_email = models.CharField(max_length=30, default='', verbose_name='邮箱')
    # 用户头像
    u_pic = models.FileField(max_length=100, upload_to='images/user/',
                             default='/static/default/timg.jpg', verbose_name='用户头像')
    # 用户等级
    u_level = models.ForeignKey(
        to='UserLevel', to_field='id', default=1,on_delete=None, verbose_name='用户等级')
    # 用户状态
    u_status = models.CharField(
        max_length=10, default='未审核', verbose_name='用户状态')
    # 用户介绍
    u_intro = models.TextField(default='这家伙很懒', verbose_name='用户介绍')
    # 注册时间
    u_addtime = models.DateTimeField(auto_now_add=True, verbose_name='注册时间')

    class Meta:
        db_table = 'mall_user'


# 会员等级表
class UserLevel(models.Model):
    # 等级名称
    l_name = models.CharField(max_length=5)

    class Meta:
        db_table = 'mall_user_level'

# 商品类别表


class Types(models.Model):
    t_name = models.CharField(max_length=30, null=False, verbose_name='类别名称')
    t_pid = models.IntegerField(default=0, null=False, verbose_name='父级栏目id')
    t_path = models.CharField(max_length=30, default='', verbose_name='类别路径')
    t_info = models.CharField(max_length=50, verbose_name='栏目简介')
    t_icon = models.CharField(
        max_length=50, default='fa-black-tie', verbose_name='栏目图标')
    t_isdel = models.IntegerField(
        default=1, verbose_name='是否删除')  # 0:删除状态  1：正常状态
    t_addtime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    t_altertime = models.DateTimeField(auto_now=True, verbose_name='最后修改时间')

    class Meta:
        db_table = 'mall_types'

# 商品表


class Goods(models.Model):
    g_name = models.CharField(max_length=255, null=False, verbose_name='商品名称')
    g_title = models.CharField(max_length=255, verbose_name='简短介绍')
    g_price = models.FloatField(null=False, verbose_name='商品价格')
    g_saleprice = models.FloatField(default=1, verbose_name='商品折扣')
    g_address = models.CharField(
        max_length=255, default='', verbose_name='商品产地')
    g_type = models.IntegerField(
        default=0, verbose_name='商品所属类别')  # 0:普通  1：新品  2：热卖
    g_mall = models.CharField(max_length=4, default='',
                              verbose_name='是否包邮')  # 0:不包邮  1：包邮
    g_tid = models.ForeignKey(to='Types', to_field='id',on_delete=None, verbose_name='商品类别')
    g_nums = models.IntegerField(default=0, verbose_name='商品数量')
    g_invnum = models.IntegerField(default=0, verbose_name='商品库存')
    g_sellnum = models.IntegerField(default=0, verbose_name='商品售卖数量')
    g_isdel = models.IntegerField(
        default=1, verbose_name='商品状态')  # 1:为显示  0：不显示
    g_intro = models.CharField(max_length=255, verbose_name='商品简介')
    g_addtime = models.DateTimeField(auto_now_add=True, verbose_name='商品添加时间')
    g_altertime = models.DateTimeField(auto_now=True, verbose_name='商品修改时间')

    class Meta:
        db_table = 'mall_goods'

# 商品信息表


class GoodsInfo(models.Model):
    i_gid = models.ForeignKey(to='Goods', on_delete=None, to_field='id')
    i_info = models.TextField(default='暂无商品详情', verbose_name='商品详情')
    i_color = models.CharField(max_length=30, default='', verbose_name='商品颜色')
    i_size = models.CharField(max_length=30, default='', verbose_name='商品尺寸')
    i_pinpai = models.CharField(max_length=30, default='', verbose_name='商品品牌')
    i_hot = models.IntegerField(default=0, verbose_name='')

    class Meta:
        db_table = 'mall_goodsinfo'

# 商品特征信息表


class GoodsAttr(models.Model):
    a_name = models.CharField(max_length=30, verbose_name='商品特征名称')
    a_pid = models.IntegerField(default=0, verbose_name='父级id')
    a_info = models.CharField(
        max_length=50, default='暂无简介', verbose_name='特征简介')
    a_isdel = models.IntegerField(
        default=1, verbose_name='特征状态')  # 1:使用状态 0 ：禁用状态
    a_path = models.CharField(max_length=30, default='', verbose_name='所属父类路径')
    a_addtime = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    a_altertime = models.DateTimeField(auto_now=True, verbose_name='修改时间')

    class Meta:
        db_table = 'mall_goodsattr'

    def __str__(self):

        return self.a_name


# 商品图片表
class Pictures(models.Model):
    p_name = models.CharField(max_length=255, verbose_name='图片名称')
    p_pic = models.ImageField(max_length=255, upload_to='images/goods/',
                              default='/static/default/timg.jpg', verbose_name='商品图片')
    p_gid = models.ForeignKey(
        to='Goods', to_field='id', on_delete=None, verbose_name='图片所属商品id')
    p_sort = models.IntegerField(default=0, verbose_name='图片排序标示')
    p_cover = models.IntegerField(
        default=0, verbose_name='封面图面标示')  # 0：普通图片  1：主图
    p_isdel = models.IntegerField(default=1, verbose_name='图片是否前台显示')
    p_addtime = models.DateTimeField(auto_now_add=True, verbose_name='图片添加时间')
    p_altertime = models.DateTimeField(auto_now=True, verbose_name='图片修改时间')

    class Meta:
        db_table = 'mall_pictures'

    # 数据转json格式


class JsonExtendEncoder(json.JSONEncoder):
    """
        This class provide an extension to json serialization for datetime/date.
    """

    def default(self, o):
        """
            provide a interface for datetime/date
        """
        if isinstance(o, datetime):
            return o.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(o, date):
            return o.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, o)

if __name__ == '__main__':
    d = {'now': datetime.now(), 'today': date.today(), 'i': 100}
    ds = json.dumps(d, cls=JsonExtendEncoder)
    print("ds type:", type(ds), "ds:", ds)
    l = json.loads(ds)
    print("l  type:", type(l), "ds:", l)
